//
//  InspirationViewController.m
//  grainHouse
//
//  Created by 北风 on 14-10-13.
//  Copyright (c) 2014年 北风. All rights reserved.
//

#import "InspirationViewController.h"
#import "LeftViewController.h"
#import "InspirationTableViewCell.h"
#import "InsData.h"
#import "InsModel.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "InsDetailViewController.h"
#import "AMScrollingNavbarViewController.h"

NSString *const MJTableViewCellIdentifier = @"Cell";



@interface InspirationViewController ()
@property(nonatomic, retain)NSMutableArray *array;
@property(nonatomic, retain)UITableView *visible;
@property (nonatomic, retain) NSMutableArray *arr;
@property(nonatomic, retain)NSMutableArray *arr1;
@property(nonatomic, assign)NSInteger count;


@end

@implementation InspirationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        InsData *ins = [[InsData alloc]init];
        
        [ins getdata:^(NSMutableArray *arry, NSMutableArray *array, NSMutableArray *array1) {
            
            //NSLog(@"%@", [arry[0] title]);
            _array = arry;
            _arr1 = array;
           // NSLog(@"%@", array1[1]);
            [self.visible reloadData];
        }];
            
            
        
        //[ins getdata];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.title = @"创意风向";
    _count = 10;
   // [self setMj];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu"                    style:UIBarButtonItemStylePlain  target:(LeftViewController *)self.navigationController
    action:@selector(showMenu)];
    
    self.visible = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375,  667 - 64) style:UITableViewStylePlain];
    _visible.delegate = self;
    _visible.dataSource = self;
    [self.view addSubview:_visible];
    [_visible release];
    
    [self.visible addHeaderWithTarget:self action:@selector(headxx) ];
    //[self.visible headerBeginRefreshing];
    
    [self.visible addFooterWithTarget:self action:@selector(footerRefreshing)];
    [self.visible footerBeginRefreshing];
    
    //[self followScrollView:_visible];
    
}

#pragma mark -
#pragma mark MJRefresh set


#pragma mark 调用刷新控件
- (void)headxx
{
    // 2.模拟2秒后刷新表格UI（真实开发中，可以移除这段gcd代码）
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.visible reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.visible headerEndRefreshing];
    });
    
    
}


- (void)footerRefreshing
{
    // 2.模拟2秒后刷新表格UI（真实开发中，可以移除这段gcd代码）

    


    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        Connect *connect = [[Connect alloc] init];
        
        //http://api.guju.com.cn/v2/ideabook?start=0&count=10&user=(null)&tag=全部
        
        NSString *string = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/ideabook?start=%ld&count=10&user=(null)&tag=全部", (long)_count ];
        //网址里不能出现汉字和空格
        NSString  *unicod = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        [connect connectWithUrl:unicod pramter:nil passValue:^(id result) {
            
            //  NSMutableArray *array = [[NSMutableArray alloc]init];
            //    NSMutableArray *array1 = [[NSMutableArray alloc]init];
            NSDictionary *dicc = [[NSDictionary alloc] init];
            
            for (NSDictionary *dic in [result objectForKey:@"ideabooks"]) {
                
                InsModel *data = [[InsModel alloc]init];
                [data setValuesForKeysWithDictionary:dic];
                dicc = [dic objectForKey:@"user"];
                //data.user = [dic objectForKey:@"user"];
                //  data.small = [data.userImage objectForKey:@"small"];
                //   [data setValuesForKeysWithDictionary:[dic objectForKey:@"user"]];
                //NSLog(@"%@", data.user);
                [_array addObject:data];
                [_arr1 addObject:dicc];
                
            }
        }];
    
        [self.visible reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.visible footerEndRefreshing];
        _count = _count + 5;
    });
    
    
}


#pragma mark -
#pragma mark tableview dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 280;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return _count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *vc =[NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];
    
    InspirationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:vc];
    
    if (cell == nil) {
        cell = [[InspirationTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:vc];
        
        
    }
//    cell.layer.shadowRadius = 7.0;
//    cell.layer.shadowOffset = CGSizeMake(0, 3);
//    cell.layer.shadowColor = [UIColor blackColor].CGColor;
//    cell.layer.shadowOpacity = 0.3;
    cell.titleLabel.text = [_array[indexPath.section] title];
    cell.timeLabel.text = (NSString *)[_array[indexPath.section] date];
    cell.nameLabel.text = [_arr1[indexPath.section] valueForKey:@"userName"];
    cell.contentLabel.text = [_array[indexPath.section] description1];
    for (int i = 0; i < 3; i++) {
        NSString *str = [_array[indexPath.section] photos][i];
        UIImageView *image1 = (UIImageView*)[cell.view1 viewWithTag:i + 1];
        [image1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w600_h0_m0.jpg", [str valueForKey:@"id"]]] placeholderImage:nil options:SDWebImageRefreshCached];
        //NSLog(@"=======%@",[_array[indexPath.section] photos][i]);
    }
    
    UIImageView *images = [[UIImageView alloc]init];
    [images sd_setImageWithURL:[[_arr1[indexPath.section] objectForKey:@"userImage"] valueForKey:@"large"] placeholderImage:nil options:SDWebImageRefreshCached];
    [cell.button setImage:images.image forState:UIControlStateNormal];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    InsDetailViewController *ins = [[InsDetailViewController alloc]init];
    UINavigationController *ncv = [[UINavigationController alloc] initWithRootViewController:ins];
    
    //属性传递
    ins.title  = [_array[indexPath.section] title];
    ins.titleTop = [_array[indexPath.section] title];
    ins.nameLabel = [_arr1[indexPath.section] valueForKey:@"userName"];
    ins.faceUrl = [[_arr1[indexPath.section] objectForKey:@"userImage"] valueForKey:@"large"];
    ins.hang = indexPath.section;
    
    ins.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:ncv animated:YES completion:nil];

    
    
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
